//
//  InvestViewController.swift
//  cxd4iphone
//
//  Created by hexy on 15/11/19.
//  Copyright © 2015年 hexy. All rights reserved.
//

import UIKit
import ReactiveCocoa
import MJRefresh


class InvestViewController: BaseTableViewController {

    private lazy var investListViewModel = InvestListViewModel()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        
        tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: { () -> Void in
            
            self.loadData(true)
        })
        
        tableView.mj_header.beginRefreshing()
        
        
        tableView.mj_footer = MJRefreshAutoNormalFooter(refreshingBlock: { () -> Void in
            
            self.loadData(false)
        })
        
    }
    private func loadData(pullDown: Bool) {
        
//        xx_showHUD(nil)
        investListViewModel.loadBidList(pullDown).subscribeError({ (error) -> Void in
            
            xx_print(error)
            }) { () -> Void in
                
//                xx_hideHUD()
                self.tableView.mj_header.endRefreshing()
                self.tableView.mj_footer.endRefreshing()
//                xx_print(self.investListViewModel.sections[0][0])
//                let mo = self.investListViewModel.sections[0][0] as! InvestCellViewModel
//                xx_print(mo.bidModel.investInfo?.nextRepayDate)
                self.tableView.reloadData()
                
                self.navigationItem.rightBarButtonItem = UIBarButtonItem(target: self, action: "showPickerView")
                
        }
    }
    func showPickerView() {
        
        let pick = PickerView()
        pick.show(investListViewModel.pickerViewModel) { (rows) -> Void in
            
            let pvm = self.investListViewModel.pickerViewModel
//            let dtitle = (pvm.components[0][rows[0]]).title
            let dmax = (pvm.components[0][rows[0]]).max
            let dmin = (pvm.components[0][rows[0]]).min
//            xx_print("\(dtitle)--\(dmin)--\(dmax)")
            
//            let rtitle = (pvm.components[1][rows[1]]).title
            let rmax = (pvm.components[1][rows[1]]).max
            let rmin = (pvm.components[1][rows[1]]).min
//            xx_print("\(rtitle)--\(rmin)--\(rmax)")
            
            let qm = self.investListViewModel.queryModel
            qm.dateMax = dmax
            qm.dateMin = dmin
            qm.rateMax = rmax
            qm.rateMin = rmin
            
            self.loadData(true)
            
        }
    }
    override func makeTableView() {
        
        self.tableListViewModel = investListViewModel
        
        tableView.registerClass(InvestListCell.self, forCellReuseIdentifier: InvestCellReuseID)
        tableView.rowHeight = xx_height(150)
        tableView.separatorStyle = .None
    }
    
    override func cellSelected(indexPath: NSIndexPath) {
        
        let vm = investListViewModel.sections[indexPath.section][indexPath.row] as! InvestCellViewModel
        
        navigationController?.pushViewController(BuyViewController(bidID: vm.bidModel.id!), animated: true)
    }

}


